<?xml version="1.0" ?>

<xsl:stylesheet   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
						xmlns:lzc="http://schemas.icitadel.com/Lazarus/Compiled.xsd"
                  version="1.0">

  <xsl:output method="text"/>

  <xsl:include href="../../common.xsl" />
  <xsl:include href="../../convert.xsl" />

  <xsl:template match="lzc:project">
    <xsl:apply-templates select="//lzc:object[ ( @compiling ) ]" />
  </xsl:template>

  <xsl:template match="lzc:object">

    <!-- ********************************************** -->
    <!-- Variable Declarations                          -->
    <!-- ********************************************** -->
    <xsl:variable name="v_ObjectName">
      <xsl:value-of select="@name" />
    </xsl:variable>

    <xsl:variable name="v_DetailName">
      <xsl:text>Derived.</xsl:text>
      <xsl:value-of select="@name" />
    </xsl:variable>
    
    <xsl:variable name="v_CollectionName">
      <xsl:value-of select="@name" />
      <xsl:text>Collection</xsl:text>
    </xsl:variable>
    
    <xsl:variable name="v_NamespacePrefix">
      <xsl:if test="/lzc:project/@namespaceprefix">
        <xsl:value-of select="/lzc:project/@namespaceprefix" />
        <xsl:text>:</xsl:text>
      </xsl:if>
    </xsl:variable>
    
    <xsl:variable name="v_RuntimeNamespace">
      <xsl:if test="/lzc:project/@defaultnamespace">
        <xsl:value-of select="/lzc:project/@defaultnamespace"/>
      </xsl:if>
    </xsl:variable>
    
    <xsl:variable name="v_BusinessConceptNamespace">
      <xsl:if test="/lzc:project/@businessconcept">
        <xsl:text>.</xsl:text>
        <xsl:value-of select="/lzc:project/@businessconcept" />
      </xsl:if>
    </xsl:variable>

    <xsl:variable name="v_DerivedNamespace">
      <xsl:value-of select="$v_RuntimeNamespace" />
      <xsl:text>.Model</xsl:text>
      <xsl:value-of select="$v_BusinessConceptNamespace" />
    </xsl:variable>

    <xsl:variable name="v_CLRNamespace">
      <xsl:value-of select="$v_RuntimeNamespace" />
      <xsl:text>.Model</xsl:text>
      <xsl:value-of select="$v_BusinessConceptNamespace" />
      <xsl:text>.Generated</xsl:text>
    </xsl:variable>

    <xsl:variable name="v_DataManagerClassName">
      <xsl:text>Derived.</xsl:text>
      <xsl:value-of select="/lzc:project/@name" />
      <xsl:text>DataManager</xsl:text>
    </xsl:variable>
    
    <xsl:variable name="v_AncestorClass">
      <xsl:text>ModelCollectionBase&lt;</xsl:text>
      <xsl:value-of select="$v_DetailName" />
      <xsl:text>, </xsl:text>
      <xsl:value-of select="$v_DataManagerClassName" />
      <xsl:text>&gt;</xsl:text>
    </xsl:variable>

    <xsl:variable name="v_ClassName">
      <xsl:value-of select="$v_CollectionName" />
      <xsl:text>Base</xsl:text>
    </xsl:variable>
    
    <xsl:variable name="v_DataAccessNamespace">
      <xsl:value-of select="$v_RuntimeNamespace" />
      <xsl:text>.DataAccess</xsl:text>
      <xsl:value-of select="$v_BusinessConceptNamespace" />
    </xsl:variable>

    <xsl:variable name="v_DataAccessClass">
      <xsl:text>DataAccess.</xsl:text>
      <xsl:value-of select="@name" />
    </xsl:variable>

    <!-- ********************************************** -->
    <!-- Output Processing                              -->
    <!-- ********************************************** -->
    <xsl:text>using System;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Xml;

using OR.Reuse.Persistence;

using DataAccess = </xsl:text>
    <xsl:value-of select="$v_DataAccessNamespace" />
    <xsl:text>;
using Derived = </xsl:text><xsl:value-of select="$v_DerivedNamespace" /><xsl:text>;
</xsl:text>

    <xsl:text>
namespace </xsl:text>
    <xsl:value-of select="$v_CLRNamespace" />
    <xsl:text>
{
    public class </xsl:text>
    <xsl:value-of select="$v_ClassName" />
    <xsl:text> : </xsl:text>
    <xsl:value-of select="$v_AncestorClass" />
    <xsl:text>
    {</xsl:text>

    <xsl:for-each select="//lzc:table[ @name = $v_ObjectName ]">
      <xsl:for-each select=".//lzc:method[ @methodtype = 'List' ]">
        
        <!-- Method name declaration. -->
        <xsl:text>
          public void </xsl:text>
        <xsl:value-of select="@name" />
        
        <!-- Method parameter list. -->
        <xsl:call-template name="createParams">
          <xsl:with-param name="fields" select="lzc:method-params/lzc:method-param" />
        </xsl:call-template>
        <xsl:text>
          {
            </xsl:text>
            <xsl:value-of select="$v_DataAccessClass" /> access<xsl:value-of select="$v_ObjectName" /> = new <xsl:value-of select="$v_DataAccessClass" />();

              DataSet result = access<xsl:value-of select="$v_ObjectName" />.<xsl:value-of select="@name" />
            <xsl:text>(</xsl:text>
            <xsl:for-each select=".//lzc:method-param">
              <xsl:if test="position() &gt; 1">, </xsl:if>
              <xsl:value-of select="@dbname" />
            </xsl:for-each>
            <xsl:text>);</xsl:text>

              Debug.Assert(result != null);
              
              if (result.Tables.Count > 0)
              {
                  DataTable sourceTable = result.Tables[0];
                  
                  foreach (DataRow currentRow in sourceTable.Rows)
                  {
                      <xsl:value-of select="$v_DetailName" /> newEntity = new <xsl:value-of select="$v_DetailName" />(this.DataManager);
                      newEntity.Load(currentRow);
                      
                      this.Add(newEntity);
                  }
              }
          }
      </xsl:for-each>

          public <xsl:value-of select="$v_ClassName" />(<xsl:value-of select="$v_DataManagerClassName" /> manager) : base(manager)
          { }
    </xsl:for-each>

    <xsl:text>
    }
}</xsl:text>
  </xsl:template>

  <xsl:template name="createParams">
    <xsl:param name="fields" />
    <xsl:choose>
      <xsl:when test="count( $fields ) &gt; 0">
        (
        <xsl:for-each select="$fields">
          <xsl:if test="@direction = 'out'">out </xsl:if>
          <xsl:call-template name="getNETParamType">
            <xsl:with-param name="pFieldType" select="@type" />
            <xsl:with-param name="pRequired" select="@required" />
          </xsl:call-template>
          <xsl:value-of select="$space" />
          <xsl:value-of select="@dbname" />
          <xsl:if test="position() &lt; count( $fields )">
            ,
          </xsl:if>
        </xsl:for-each>
        )
      </xsl:when>
      <xsl:when test="count( $fields ) = 0">()</xsl:when>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>